#include "music.h"
#include<QUuid>
#include<QMediaPlayer>
#include<QCoreApplication>
#include<QDebug>
#include<QString>
#include<QSqlDatabase>
#include<QSqlError>
#include<QSqlQuery>
Music::Music()
{

}

Music::Music(const QUrl &as)
    :isLike(false)
    ,isHistory(false)
    ,musicUrl(as)
{
     musicId=QUuid::createUuid().toString();
     parseMediaMetaData();
}

void Music::setIsLike(bool isLike)
{
    this->isLike = isLike;
}

void Music::setIsHistory(bool isHistory)
{
    this->isHistory = isHistory;
}

void Music::setMusicName(const QString &musicName)
{
    this->musicName = musicName;
}

void Music::setSingerName(const QString &singerName)
{
    this->signerName = singerName;
}

void Music::setAlbumName(const QString &albumName)
{
    this->albumName = albumName;
}

void Music::setDuration(const qint64 duration)
{
    this->duration = duration;
}

void Music::setMusicUrl(const QUrl &url)
{
    this->musicUrl = url;
}

void Music::setMusicId(const QString &musicId)
{
    this->musicId = musicId;
}

void Music::InsertMusicDB()
{
    QSqlQuery query;
    query.prepare("SELECT EXISTS (SELECT 1 FROM musicInfo WHERE musicid = ?)");
    query.addBindValue(musicId);
    if(!query.exec())
    {
        qDebug()<<"查询失败: "<<query.lastError().text();
        return;
    }
    if(query.next())
    {
        bool as=query.value(0).toBool();
        if(as)
        {
            query.prepare("UPDATE musicInfo SET isLike = ?,isHistory = ? WHERE musicId = ?");
            query.addBindValue(isLike);
            query.addBindValue(isHistory);
            query.addBindValue(musicId);
            if(!query.exec())
            {
                qDebug()<<"更新失败: "<<query.lastError().text();
            }

            qDebug()<<"更新music信息: "<<musicName<<" "<<musicId;
        }
        else
        {
            query.prepare("INSERT INTO musicInfo(musicId, musicName, \
                          musicSinger, albumName, musicUrl,\
                          duration, isLike, isHistory) VALUES(?,?,?,?,?,?,?,?)");
            query.addBindValue(musicId);
            query.addBindValue(musicName);
            query.addBindValue(signerName);
            query.addBindValue(albumName);
            query.addBindValue(musicUrl.toLocalFile());
            query.addBindValue(duration);
            query.addBindValue(isLike?1:0);
            query.addBindValue(isHistory?1:0);
            if(!query.exec())
            {
                qDebug()<<"插⼊失败: "<<query.lastError().text();
                return;
            }

            qDebug()<<"插⼊music信息: "<<musicName<<" "<<musicId;
        }
    }
}

bool Music::getIsLike()
{
    return isLike;
}

bool Music::getIsHistory()
{
    return isHistory;
}

QString Music::getMusicName()
{
    return musicName;
}

QString Music::getSingerName()
{
    return signerName;
}

QString Music::getAlbumName()
{
    return albumName;
}

qint64 Music::getDuration()
{
    return duration;
}

QUrl Music::getMusicUrl()
{
    return musicUrl;
}

QString Music::getMusicId()
{
    return musicId;
}

QString Music::getLrcFilePath()
{
    QString path= musicUrl.toLocalFile();
    path.replace(".mp3",".lrc");
    path.replace(".flac",".lrc");
    path.replace(".mpga",".lrc");
    return path;
}

void Music::parseMediaMetaData()
{
    QMediaPlayer player;
    player.setMedia(musicUrl);
    while(!player.isMetaDataAvailable())
    {
        QCoreApplication::processEvents();
    }
    if(player.isMetaDataAvailable())
    {
        musicName=player.metaData("Title").toString();
        signerName = player.metaData("Author").toString();
        albumName = player.metaData("AlbumTitle").toString();
        duration = player.metaData("Duration").toLongLong();
        qDebug()<<musicName<<":"<<signerName<<":"<<albumName<<":"<<duration;
        QString fileName=musicUrl.fileName();
        int index=fileName.indexOf('-');
        if(musicName.isEmpty())
        {
            if(index!=-1)
            {
                musicName=fileName.mid(0,index).trimmed();
            }
            else
            {
                musicName = fileName.mid(0,fileName.indexOf('.')).trimmed();
            }

        }

        if(signerName.isEmpty())
        {
            if(index!=-1)
            {
                signerName=fileName.mid(index+1,fileName.indexOf('.')-index-1).trimmed();
            }
            else
            {
                signerName ="未知歌手";
            }

        }

         if(albumName.isEmpty())
         {
             albumName="未知专辑";

         }
       qDebug()<<musicName<<" "<<signerName<<" "<<albumName<<" "<<duration;
    }
}
